<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","nobkt");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_advsummary.php";
$FileTemplate  = "ais_transaction_advsummary_list.html";
$TableName     = "ais_advance";
$SubHeaderTitle= "Transaction Advance - Entry Advance";
$PageSize      = 10 ;
$HTMLGridList  = "AdvanceGrid";
//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }

//Initiate Database Table For Search;
$SQLLedger = "SELECT ledger FROM ais_history_ledger ORDER BY ledger DESC LIMIT 1";
$FCode = GetParam("advCodeSearch","");
//$FLedger = (strlen(GetParam("ledger",""))>0)?GetParam("ledger",""):$DBConnection->dbc->get_var($SQLLedger);
$FLedger = GetParam("ledger","");

//$FSkada = GetParam("skadaSearch","");
//$FDept = GetParam("deptSearch","");
$QueryGet  = GetQueryString("All", array('PageNum'));
//******* DATABASE FORM QUERY ***********
//$SQL  = "SELECT $TableName.*, sum($TableName.jumlah) as sumjumlah, IF (bb_bk.nobkt<>'',bb_bk.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as aslref FROM `$TableName` ";
/*$SQL  = "SELECT $TableName.*, (SELECT sum(a.jumlah) FROM $TableName a WHERE a.nobkt=$TableName.nobkt) as sumjumlah, IF (bb_bk.nobkt<>'',bb_bk.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as referensi FROM `$TableName` ";
$SQL .= "LEFT JOIN ais_memorial bb_bk ON $TableName.nobkt=bb_bk.noref AND bb_bk.urut=999 ";
$SQL .= "LEFT JOIN ais_memorial_z LZ ON $TableName.nobkt=LZ.noref AND bb_bk.urut=999 ";*/
//$SQLCount = "SELECT count(*) FROM `$TableName` ";




//$SQL  = "SELECT $TableName.*, a.curr as currencycost, a.rate as ratecost, sum(a.total_amount) as jumlahcost, (SELECT sum(a.jumlah) FROM $TableName a WHERE a.nobkt=$TableName.nobkt) as sumjumlah, IF (bb_bk.nobkt<>'',bb_bk.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as referensi FROM `$TableName` ";
$SQL  = "SELECT $TableName.*, a.curr as currencycost, a.rate as ratecost, sum(ais_advance.jumlah) as jumlahcost, (SELECT sum(a.jumlah) FROM $TableName a WHERE a.nobkt=$TableName.nobkt) as sumjumlah, IF (bb_bk.nobkt<>'',bb_bk.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as referensi FROM `$TableName` ";
$SQL .= "LEFT JOIN tbljoborder_costing a ON $TableName.jo_costing_id=a.jo_costing_id ";
$SQL .= "LEFT JOIN ais_memorial bb_bk ON $TableName.nobkt=bb_bk.noref AND bb_bk.urut=999 ";
$SQL .= "LEFT JOIN ais_memorial_z LZ ON $TableName.nobkt=LZ.noref AND bb_bk.urut=999 ";

$WHERE  = "$TableName.ktrasl='".GetOfficeID()."' AND ais_advance.bs_progress <> 'cancel'";

//Retrieve Parameter from search
/*
if ($FLedger) {
    $CustomWhere = " $TableName.ledger like '".$FLedger."'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}
*/


if ($FCode) {
    $CustomWhere = " $TableName.nobkt like '".$FCode."%'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}


if (strlen($WHERE)) $SQL = $SQL."WHERE ".$WHERE;
else $SQL = $SQL;
$SQL = $SQL." GROUP BY $TableName.nobkt ";
$SQL .= " ORDER BY ".$TableName.".adv_id DESC ";



//SQL COUNT
$SQLCount  = "SELECT count(*) FROM `$TableName` ";
$SQLCount .= "LEFT JOIN tbljoborder_costing a ON $TableName.jo_costing_id=a.jo_costing_id ";
$SQLCount .= "LEFT JOIN ais_memorial bb_bk ON $TableName.nobkt=bb_bk.noref AND bb_bk.urut=999 ";
$SQLCount .= "LEFT JOIN ais_memorial_z LZ ON $TableName.nobkt=LZ.noref AND bb_bk.urut=999 ";

$WHERE  = "$TableName.ktrasl='".GetOfficeID()."' AND ais_advance.bs_progress <> 'cancel' ";

if ($FCode) {
    $CustomWhere = " $TableName.nobkt like '".$FCode."%'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}


if (strlen($WHERE)) $SQLCount = $SQLCount."WHERE ".$WHERE;
else $SQLCount = $SQLCount;
$SQLCount = $SQLCount." GROUP BY $TableName.nobkt";






//print $SQL;
//Select Customer
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
//$SQLCount .= " WHERE urut=1 ";
/*
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;
if (!$RecCnt) $RecCnt=0;
*/
if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
}
$SQLLedger = "SELECT * FROM ais_history_ledger ORDER BY ledger DESC";

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("ledgerblk",$DBMysql,$SQLLedger);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName,$TableName;
    for ($x=1;$x<=$PageSize;$x++) {
        if (GetParam($sequence_name.$x,"")) {
            $DBConnection->Delete("$TableName",$sequence_name.$x);
        }
    }
    header("Location: $FileName");
    exit;

}

?>
